package com.car.account.rabbitmq;

import com.car.account.util.SMSUtil;
import com.car.commons.web.util.RedisKeyUtil;
import com.car.commons.web.util.RedisUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RabbitListener(queues = "send_login_code_queue")
public class SendLoginCodeConsumer {
    @Resource
    private SMSUtil smsUtil;
    @Resource
    private RedisUtil redisUtil;
    @RabbitHandler
    public void sendLoginCode(String phone) throws Exception{
        //发送验证码
        String code = smsUtil.sendCode(phone);
        //将验证码设置2分钟有效时间
        redisUtil.set(RedisKeyUtil.loginCodeKey(phone),code,2*60);
        //设置1分钟限制发送的Redis键
        redisUtil.set(RedisKeyUtil.loginCodeOnMinute(phone), "1", 60);
        //一小时内发送次数加1
        redisUtil.incr(RedisKeyUtil.sendNumbers(phone));
    }
}
